﻿using System;

namespace Sample
{
    public class CombatRatio : IEquatable<CombatRatio>
    {
        public CombatRatio(Int32 attacker, Int32 defender)
        {
            if (attacker == defender)
            {
                this.AttackerOdds = 1;
                this.DefenderOdds = 1;
                return;
            }

            Decimal attackerValue = attacker;
            Decimal defenderValue = defender;

            Decimal ratio = attackerValue/defenderValue;
            if (attacker < defender)
            {
                this.AttackerOdds = 1;
                this.DefenderOdds = Convert.ToInt32(Math.Floor(1/ratio));
                if (this.DefenderOdds > 4) this.DefenderOdds = 4;
            }
            else
            {
                this.DefenderOdds = 1;
                this.AttackerOdds = Convert.ToInt32(Math.Floor(ratio));
                if (this.AttackerOdds > 7) this.AttackerOdds = 7;
            }
        }

        public Int32 AttackerOdds
        {
            get; private set;
        }

        public Int32 DefenderOdds
        {
            get; private set;
        }

        public Boolean Equals(CombatRatio other)
        {
            if (other == null) return false;
            return (this.AttackerOdds == other.AttackerOdds && this.DefenderOdds == other.DefenderOdds);
        }

        public override Boolean Equals(Object obj)
        {
            return obj is CombatRatio ? this.Equals((CombatRatio) obj) : false;
        }

        public override Int32 GetHashCode()
        {
            unchecked
            {
                return this.AttackerOdds.GetHashCode() + this.DefenderOdds.GetHashCode();
            }
        }
    }
}
